//获取应用实例
const app = getApp();
var click = true;
Page({
	data:{
   		phoneInput:'',
		smstext:'获取验证码'
  	},
  	phoneInput:function(e){
		this.setData({
			phoneInput:e.detail.value
		})
	},
	onLoad:function(){	
   		
  	},
	formSubmit:function(e){	
		if(!e.detail.value.phone){
			wx.showToast({
				title:'请输入手机号',
				icon:'none'
			})
			return false;
		}
		if(e.detail.value.phone.length != 11){
			wx.showToast({
				title:'请输入正确手机号',
				icon:'none'
			})
			return false;
		}
  		if(!e.detail.value.password){
			wx.showToast({
				title:'请输入验证码',
				icon:'none'
			})
			return false;
		}
  		if(e.detail.value.password.length != 6){
			wx.showToast({
				title:'请输入正确验证码',
				icon:'none'
			})
			return false;
		}
		app.request('/api/Simple/register', {phone:e.detail.value.phone,code:e.detail.value.password}, function(res){
			wx.showToast({
				title:res.msg,
				icon:'none'
			})
			if(res.code == '0'){
				wx.navigateTo({
				    url:'/pages/personinfo/personinfo'
				})
			}
		});
	},
	formReset:function(){
    
  	},
  	//获取验证码
  	getCode:function(){
		var that  = this;
		if(click === true){
			if(that.data.phoneInput && that.data.phoneInput.length==11){
				app.request('/api/Simple/getCode', {phone:that.data.phoneInput}, function(res){
					click = false;
					var i = 60;
					if(res.code == '0'){
						that.setData({
							smstext:i+'S'
						})
						var t = setInterval(function(){
							i--;
							if(i > -1){
								that.setData({
									smstext:i+'S'
								})
							}else{
								clearInterval(t);
								that.setData({
									smstext:'获取验证码'
								})
								click = true;
							}
						}, 1000);
					}else{
						wx.showToast({
							title:res.msg,
							icon:'none'
						})
						click = true;
					}
				});			
			}else{
				wx.showToast({
					title:'请输入正确手机号',
					icon:'none'
				})
			}
		}
  	},
  	//绑定会员
  	bianMeber:function(){
		wx.navigateTo({
			url: '/pages/registerMeber/registerMeber'
		})
  	},
  	backIndex:function(){
  		wx.switchTab({
		    url:'/pages/index/index'
		})
  	}
})
